package firing;

import game.Bullet;
import game.Enemy;
import game.Player;

import java.util.List;

/** Node that fires a bullet */
public class FireNode extends AbstractUnaryNode {
    
    private static final long serialVersionUID = 2354833002169009593L;
    private int bulletSize;
    
    public FireNode() {
	super();
	randomizeNode();
    }

    /** Simply fires a bullet if able, and continues down tree */
    public void calculate(List<Bullet> bullets, double angle, Enemy shooter, Player player) {
	if (shooter.getEnergy() >= Bullet.energyCost[bulletSize]){ 
	    bullets.add(new Bullet(shooter.getX(), shooter.getY(), angle, bulletSize));
	    shooter.resetLastFired();
	    shooter.setEnergy(shooter.getEnergy() - Bullet.energyCost[bulletSize]);
	} else {
	    shooter.wasteEnergy();
	}
	if (child != null) {
	    child.calculate(bullets, angle, shooter, player);
	}
    }

    @Override
    public void randomizeNode() {
	bulletSize = (int)(Math.random() * 3);
    }
    
    public int getBulletSize() {
	return bulletSize;
    }
    
    public void setBulletSize(int size) { 
	this.bulletSize = size;
    }
    
    public String toString() {
	return ("Fire " + Bullet.toString(bulletSize));
    }

    @Override
    public Node duplicate() {
	FireNode duplicate = new FireNode();
	duplicate.bulletSize = bulletSize;
	if (child != null) {
	    duplicate.child = child.duplicate();
	}
	return duplicate;
    }
}
